package fonkin.com.wifisignapp.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import fonkin.com.wifisignapp.R;
import fonkin.com.wifisignapp.acticity.addressList.SearchUser;
import fonkin.com.wifisignapp.acticity.news.NewsDetail;
import fonkin.com.wifisignapp.acticity.proceeding.DeptUserDetail;
import fonkin.com.wifisignapp.adapter.news.DeptAdapter;
import fonkin.com.wifisignapp.adapter.news.NewsAdapter;
import fonkin.com.wifisignapp.adapter.news.UserAdapter;
import fonkin.com.wifisignapp.bean.news.NewsInfor;
import fonkin.com.wifisignapp.bean.tissue.Department;
import fonkin.com.wifisignapp.bean.user.User;
import fonkin.com.wifisignapp.retrofit.BaseRetrofit;
import fonkin.com.wifisignapp.utils.PrintLog;
import fonkin.com.wifisignapp.utils.ReturnVo;
import fonkin.com.wifisignapp.utils.ShowToast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class ProceedingFragment extends Fragment {
    TextView textView;
    ListView dept_list_infor,dept_user_list;
    List<Department> deptsList = new ArrayList<>();
    DeptAdapter deptAdapter;
    UserAdapter userAdapter;
    ImageView imageView;

    List<User> userList = new ArrayList<>();

    public static ProceedingFragment newInstance(String text){
        ProceedingFragment proceedingFragment=new ProceedingFragment();
        Bundle bundle=new Bundle();
        bundle.putString("text",text);
        proceedingFragment.setArguments(bundle);
        return proceedingFragment;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.proceeding_fragment,container,false);
        textView= (TextView) view.findViewById(R.id.textView);
        textView.setText(getArguments().getString("text"));

        return view;
    }



    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        dept_list_infor  = (ListView)getView().findViewById(R.id.dept_listView);
        dept_user_list  = (ListView)getView().findViewById(R.id.user_listView);
        imageView = (ImageView)getView().findViewById(R.id.search_btn);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SearchUser.class);
                startActivity(intent);
            }
        });
        initDept();
    }



    public void initDept(){
        deptAdapter = new DeptAdapter(getActivity(),new ArrayList<Department>());
        dept_list_infor.setAdapter(deptAdapter);
        BaseRetrofit.GetBaseRetrofit().listDeptsUser().enqueue(new Callback<ReturnVo<List<Department>>>() {
            @Override
            public void onResponse(Call<ReturnVo<List<Department>>> call, Response<ReturnVo<List<Department>>> response) {
                PrintLog.printApi(response,call);
                if (response.isSuccessful()){
                    if (response.body().getCode().equals(1)){
                        deptsList = response.body().getData();
                        deptAdapter.addAll(deptsList);
                        setListViewHeightBasedOnChildren(dept_list_infor);
                        dept_list_infor.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Department department = deptsList.get(position);
                                userList = department.getChildrenUser();
                                initUserList(department.getChildrenUser());
                            }
                        });
                    }
                }else {

                }
            }

            @Override
            public void onFailure(Call<ReturnVo<List<Department>>> call, Throwable t) {

            }
        });

    }

    public void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

    public void initUserList(List<User> users){
        userAdapter = new UserAdapter(getActivity(),new ArrayList<User>());
        dept_user_list.setAdapter(userAdapter);
        userAdapter.addAll(users);
        setListViewHeightBasedOnChildren(dept_user_list);
        dept_user_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                User userDetail = userList.get(position);
                Intent intent = new Intent(getActivity(), DeptUserDetail.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("userDetail", (Serializable) userDetail);
                intent.putExtras(bundle);
                startActivity(intent);

            }
        });
    }
}
